package com.dz.canal.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * ming
 *
 * @author ming
 * @Date
 **/
@Component
@Slf4j
public class RedisComponent {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    public void setObj(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }
    public void setExpireObj(String key, String value, long timeout) {
        redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
    }
    public String getObj(String key) {
        try {
            return redisTemplate.opsForValue().get(key);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }
        return null;
    }
    public Set<String> getKeys(String key) {
        try {
            return redisTemplate.keys(key+"*");
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }
        return null;
    }
    public Long getExpire(String key) {
        try {
            return redisTemplate.getExpire(key);
        } catch (Exception e) {
            log.error(e.getLocalizedMessage());
        }
        return null;
    }
    public void expire(String key, Long timeout) {
        redisTemplate.expire(key, timeout, TimeUnit.SECONDS);;
    }
    public void del(String key) {
        redisTemplate.delete(key);
    }
}
